package tutorial.jmh;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class TestCicli {
	
	private int numCicli=100000;
	
	
    @Benchmark
    public String da0aNumCicli() 
	{
		for (int i = 0; i < numCicli; i++) 
		{
			i=i+1-1;
		}
        return "asd" ;
    }
 
    @Benchmark
    public String daNumCiclia0() 
	{
		for (int i = numCicli-1; i > 0; i--) 
		{
			i=i+1-1;
		}
        return "asd" ;
    }	
    
    
    @Benchmark
    public String forWithMethodCall() 
	{
    	Cicli c = new Cicli();
    	
		for (int i = 0; i < c.getNumCicli(); i++) 
		{
			i=i+1-1;
		}
        return "asd" ;
    }
    
    @Benchmark
    public String forWithoutMethodCall() 
	{
    	Cicli c = new Cicli();
    	int nc = c.getNumCicli();
    	
		for (int i = 0; i < nc; i++) 
		{
			i=i+1-1;
		}
        return "asd" ;
    }    
    
    class Cicli{
    	int numCicli = 10000;
    	
    	public int getNumCicli(){
    		return this.numCicli;
    	}
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(".*" + TestCicli.class.getSimpleName() + ".*")
                .mode(Mode.Throughput)
                .warmupIterations(2)
                .measurementIterations(10)
                .forks(1)
                .build();

        new Runner(opt).run();
    }

}
